package com.raymond.service;

import com.raymond.entity.employee.EmployeeEntity;
import com.raymond.entity.commons.PageResponse;
import com.raymond.mapper.employee.EmployeeMapper;
import com.raymond.entity.employee.EmployeePageQueryReqVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Random;

/**
 * desc-
 *
 * @author houmenghe
 * @date 2025/04/16 19:00
 **/
@Service
@Slf4j
public class EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    public EmployeeEntity get(Long id) {
        return employeeMapper.selectEmployeeById(id);
    }

    public void saveRandom() {
        EmployeeEntity insert = new EmployeeEntity();
        Random random = new Random();
        int i = random.nextInt(100);
        insert.setUsername("zs" + i);
        insert.setPassword("123456zs" + i);
        insert.setEmail("123456zs" + i + "@qq.com");
        insert.setHeadImage("https://bkimg.cdn.bcebos.com/pic/11385343fbf2b2119313eca125d272380cd79023209b");
        insert.setAge(i);
        employeeMapper.insertEmployee(insert);
    }

    public void save(EmployeeEntity entity) {
        employeeMapper.insertEmployee(entity);
    }

    public void update(EmployeeEntity entity) {
        Assert.notNull(entity.getId(), "id不能为空");
        employeeMapper.updateEmployee(entity);
    }

    public PageResponse<EmployeeEntity> searchList(EmployeePageQueryReqVo query) {
        PageResponse<EmployeeEntity> page = PageResponse.create(query);
        List<EmployeeEntity> employeeList = employeeMapper.searchList(query);
        if (CollectionUtils.isEmpty(employeeList)) {
            return page;
        }
        page.setDatas(employeeList);
        if (query.isQueryTotal()) {
            page.setTotal(employeeMapper.count(query));
        }
        return page;
    }

    public void delete(Long id) {
        employeeMapper.deleteEmployeeById(id);
    }
}
